package com.cxy.redisclient.presentation.string;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.cxy.redisclient.integration.I18nFile;
import com.cxy.redisclient.presentation.RedisClient;
import com.cxy.redisclient.presentation.component.DataContent;
import com.cxy.redisclient.service.NodeService;

public class StringDataContent extends DataContent {
	private NodeService service = new NodeService();
	private String value;
	private Button btnOk;
	private Label label;
	private Button btnCancel;
	
	public StringDataContent(CTabItem tabItem, int id, String server, int db, String key,
			String dataTitle) {
		super(tabItem, id, server, db, key, dataTitle);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void initData(Composite dataComposite) {
		label = new Label(dataComposite, SWT.NONE);
		label.setText(RedisClient.i18nFile.getText(I18nFile.VALUE));
		value = service.readString(id, db, key);

		final StyledText text_value = new StyledText(dataComposite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
		GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
		gd.heightHint = 120;
		text_value.setLayoutData(gd);
		text_value.setWordWrap(true);
		text_value.setText(value);
		text_value.selectAll();
		text_value.setFocus();

		new Label(dataComposite, SWT.NONE);
		new Label(dataComposite, SWT.NONE);
		new Label(dataComposite, SWT.NONE);
		
		Composite composite = new Composite(dataComposite, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		composite.setLayout(new GridLayout(2, false));

		btnOk = new Button(composite, SWT.NONE);
		btnOk.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		btnOk.setText(RedisClient.i18nFile.getText(I18nFile.APPLY));
		setApply(false);
		
		
		btnCancel = new Button(composite, SWT.NONE);
		btnCancel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		btnCancel.setEnabled(false);
		btnCancel.setText(RedisClient.i18nFile.getText(I18nFile.CANCEL));
		
		text_value.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String newValue = text_value.getText() == null?"":text_value.getText();
				if(newValue.equals(value)) {
					setApply(false);
					btnCancel.setEnabled(false);
				}
				else{
					setApply(true);
					btnCancel.setEnabled(true);
				}
			}
		});
		
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String key = getKey();
				value = text_value.getText();
				
				service.updateString(id, db, key, value);
				setApply(false);
				btnCancel.setEnabled(false);
			}
		});
		
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_value.setText(value);
				setApply(false);
				btnCancel.setEnabled(false);
			}
		});
		
	}

	@Override
	public void refreshLangUI() {
		label.setText(RedisClient.i18nFile.getText(I18nFile.VALUE));
		btnOk.setText(RedisClient.i18nFile.getText(I18nFile.APPLY));
		btnCancel.setText(RedisClient.i18nFile.getText(I18nFile.CANCEL));
		super.refreshLangUI();
	}

	public boolean isApply() {
		return btnOk.isEnabled();
	}
	@Override
	public Button getApplyButtion() {
		return btnOk;
	}
}
